package com.school.admin.common.config;

import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RMap;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * @program: school-java-mall
 * @description: Redsson配置类
 * @author: sunpx
 * @create: 2021-10-04 18:43
 **/
@Slf4j
@Configuration
public class RedssonConfig {

/*    @Value("${spring.redis.host}")
    String redisHost;

    @Value("${spring.redis.port}")
    String redisPort;

    @Value("${spring.redis.password}")
    String redisPass;

    @Value("${spring.redis.database}")
    String database;*/

   /* @Bean(destroyMethod = "shutdown")
    public RedissonClient redissonClient() {
        Config config = new Config();
        config.useSingleServer() // 使用单机模式
                .setAddress("redis://" + redisHost + ":" + redisPort)
                .setDatabase(Integer.parseInt(database))
                .setKeepAlive(true)
                // 设置1秒钟ping一次来维持连接
                .setPingConnectionInterval(1000)
                .setPassword(redisPass);
        return Redisson.create(config);
    }*/


    @Bean(destroyMethod = "shutdown")
    public RedissonClient redisson() throws IOException {
        RedissonClient redissonClient = Redisson.create(Config.fromYAML(new ClassPathResource("redisson-single.yml").getInputStream()));
        RMap map = redissonClient.getMap("a");
        map.expire(3000, TimeUnit.MICROSECONDS);
        log.info("=========Redsson配置加载完成===========");
        return redissonClient;

    }

}
